//
//  SetViewController.m
//  新项目
//
//  Created by 吴品泽 on 15/4/2.
//  Copyright (c) 2015年 吴品泽. All rights reserved.
//

#import "SetViewController.h"
#import <AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>

#import "DesitionModel1.h"
#import "DesitionModel2.h"
#import "DesitionCell.h"
#import "DesitionCell1.h"
@interface SetViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,retain)UIView *buttonView;
@property (nonatomic,retain)UIButton *leftButton;
@property (nonatomic,retain)UIButton *rightButton;
@property (nonatomic,retain)UIView *backView;


@property (nonatomic,retain)UIView *view1;
@property (nonatomic,strong)UICollectionView *MycollectionView;
@property (nonatomic,strong)UICollectionView *MycollectionView1;



@property (nonatomic,retain)UIView *view2;


@property (nonatomic,strong)DesitionModel1 *mode1;
@property (nonatomic,strong)NSMutableArray *arr1;

@property (nonatomic,strong)NSMutableArray *guowaiArr;
@property (nonatomic,strong)NSMutableArray *guoneiArr;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arr1 = [NSMutableArray array];
    self.guoneiArr = [NSMutableArray array];
    self.guowaiArr =  [NSMutableArray array];
    [self createTwoButton];
    [self createView1AndView2];
    [self getrequst];
    [self createCollectionView];
}


- (void)getrequst
{
    NSString *string = @"https://chanyouji.com/api/destinations.json";
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSMutableDictionary *dic in responseObject) {
            DesitionModel1 *mode = [[DesitionModel1 alloc]init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.arr1 addObject:mode];
            [mode getDesitionModel2];
            
        }
        
        NSMutableArray *array1 = [NSMutableArray array];
        for (NSInteger i = 0 ; i < 3; i++) {
            DesitionModel1 *mo = [[DesitionModel1 alloc]init];
            mo = [self.arr1 objectAtIndex:i];
            [array1 addObject:mo];
        }
        
        for (int i = 0; i < 3; i++) {
            for (DesitionModel2 *modf in [[array1 objectAtIndex:i] destinations]) {
                [self.guowaiArr addObject:modf];
            }
        }
        
        
        
        NSLog(@">>>>>国外%d",self.guowaiArr.count);
        
        NSMutableArray *array2 = [NSMutableArray array];
        for (NSInteger i = 3 ; i < 5; i++) {
            DesitionModel1 *mo = [[DesitionModel1 alloc]init];
            mo = [self.arr1 objectAtIndex:i];
            [array2 addObject:mo];
        }
        
        
        for (int i = 0; i < 2; i++) {
            for (DesitionModel2 *modf in [[array2 objectAtIndex:i] destinations]) {
                [self.guoneiArr addObject:modf];
            }
        }
        

        NSLog(@">>>>>国内%d",self.guoneiArr.count);
        

        
      //  NSLog(@"<<<<%@",[[[[self.guoneiArr objectAtIndex:0] destinations] objectAtIndex:0] name_zh_cn]);
        
        [self.MycollectionView reloadData];
        [self.MycollectionView1 reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求出错了");
    }];
    
    
    
}

- (void)createCollectionView
{
    NSLog(@"MMMMM%f",HIGHT);
    UICollectionViewFlowLayout *layoutq = [[UICollectionViewFlowLayout alloc]init];
    layoutq.itemSize = CGSizeMake(WIDTH / 2.286, HIGHT / 3.155);
    layoutq.minimumLineSpacing = 10;
   // layoutq.minimumInteritemSpacing = 10;
    
    layoutq.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    
    self.MycollectionView = [[UICollectionView alloc]initWithFrame:self.view1.bounds collectionViewLayout:layoutq];
    self.MycollectionView.backgroundColor = [UIColor whiteColor];
    
    self.MycollectionView.delegate =self;
    self.MycollectionView.dataSource = self;
    
    [self.view1 addSubview:self.MycollectionView];
    
    [self.MycollectionView registerClass:[DesitionCell class] forCellWithReuseIdentifier:@"identfi"];
    
    
    
    UICollectionViewFlowLayout *layoutq1 = [[UICollectionViewFlowLayout alloc]init];
    layoutq1.itemSize = CGSizeMake(WIDTH / 2.286, HIGHT / 3.155);
    layoutq1.minimumLineSpacing = 10;
    // layoutq.minimumInteritemSpacing = 10;
    
    layoutq1.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    
    self.MycollectionView1 = [[UICollectionView alloc]initWithFrame:self.view2.bounds collectionViewLayout:layoutq1];
    self.MycollectionView1.backgroundColor = [UIColor whiteColor];
    self.MycollectionView1.delegate =self;
    self.MycollectionView1.dataSource = self;
    
    [self.view2 addSubview:self.MycollectionView1];
    
    [self.MycollectionView1 registerClass:[DesitionCell class] forCellWithReuseIdentifier:@"ident"];
    
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.MycollectionView) {
        NSLog(@"__国外%d",self.guowaiArr.count);
        return self.guowaiArr.count;

    }else if (collectionView == self.MycollectionView1){
        NSLog(@"__国内%d",self.guoneiArr.count);
               return self.guoneiArr.count;
    }else{
        return 0;
    }
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.MycollectionView) {
        DesitionCell *cell = [self.MycollectionView dequeueReusableCellWithReuseIdentifier:@"identfi" forIndexPath:indexPath];
       // NSLog(@"~~~111");
       DesitionModel2 *mode = [self.guowaiArr objectAtIndex:indexPath.item];
        NSURL *url = [NSURL URLWithString:mode.image_url];
        
#pragma 占位图没加
        [cell.myimageView sd_setImageWithURL:url placeholderImage:nil];
        
        cell.contreLabel.text = mode.name_zh_cn;
        cell.contreLabel.font = [UIFont systemFontOfSize:18];
        
        cell.pinyingLabel.text = mode.name_en;
        cell.pinyingLabel.font = [UIFont systemFontOfSize:16];
        
        NSString *stt = [NSString stringWithFormat:@"旅行地 %@",mode.poi_count];
        cell.destLabel.text = stt;
        cell.destLabel.font = [UIFont boldSystemFontOfSize:12];
        
        return cell;
    }else if (collectionView == self.MycollectionView1){
        DesitionCell1 *cell = [self.MycollectionView1 dequeueReusableCellWithReuseIdentifier:@"ident" forIndexPath:indexPath];
      //  NSLog(@"~~~222");
        DesitionModel2 *mode = [self.guoneiArr objectAtIndex:indexPath.item];
        NSURL *url = [NSURL URLWithString:mode.image_url];
        
#pragma 占位图没加
        [cell.myimageView sd_setImageWithURL:url placeholderImage:nil];
        
        cell.contreLabel.text = mode.name_zh_cn;
        cell.contreLabel.font = [UIFont systemFontOfSize:18];
        
        cell.pinyingLabel.text = mode.name_en;
        cell.pinyingLabel.font = [UIFont systemFontOfSize:16];
        
        NSString *stt = [NSString stringWithFormat:@"旅行地 %@",mode.poi_count];
        cell.destLabel.text = stt;
        cell.destLabel.font = [UIFont boldSystemFontOfSize:12];
        

        return cell;
    }else{
        return nil;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.MycollectionView) {
         DesitionModel2 *mode = [self.guowaiArr objectAtIndex:indexPath.item];
        JipanViewController *jipanVc = [[JipanViewController alloc] init];
        jipanVc.number = [mode.ID stringValue];
        [self.navigationController pushViewController:jipanVc animated:YES];
        
    }
    else if(collectionView == self.MycollectionView1)
    {
        DesitionModel2 *mode = [self.guoneiArr objectAtIndex:indexPath.item];
        JipanViewController *jipanVc = [[JipanViewController alloc] init];
        jipanVc.number = [mode.ID stringValue];
        [self.navigationController pushViewController:jipanVc animated:YES];
        
        
    }
    
}

//创建精选顶部的两个button
- (void)createTwoButton
{
    //按钮的底部视图
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(0,64,WIDTH, 40)];
    self.buttonView.userInteractionEnabled = YES;
    
    UIView  *underButtonView = [[UIView alloc]initWithFrame:CGRectMake(10, 5, WIDTH - 20, self.buttonView.frame.size.height - 10)];
    underButtonView.layer.masksToBounds = YES;
    underButtonView.layer.cornerRadius = 5;
    underButtonView.userInteractionEnabled = YES;
    //左侧按钮的设置
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.leftButton.frame = CGRectMake(0, 0, underButtonView.frame.size.width / 2, underButtonView.frame.size.height);
    [self.leftButton setTitle:@"国外" forState:UIControlStateNormal];
    //self.leftButton.layer.masksToBounds = YES;
    // self.leftButton.layer.cornerRadius = 5;
    
    //右侧按钮的设置
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.rightButton.frame = CGRectMake(underButtonView.frame.size.width / 2, 0, underButtonView.frame.size.width / 2, underButtonView.frame.size.height);
    [self.rightButton setTitle:@"国内" forState:UIControlStateNormal];
    
    self.rightButton.tintColor = [UIColor blackColor];
    self.leftButton.tintColor = [UIColor blueColor];
    
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(1, 1, underButtonView.frame.size.width / 2 - 1, underButtonView.frame.size.height - 2)];
    self.backView.userInteractionEnabled = YES;
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.cornerRadius = 5;
    
    self.backView.backgroundColor = [UIColor whiteColor];
    underButtonView.backgroundColor = [UIColor colorWithRed:230 / 255.f green:230 / 255.f blue:230 / 255.f alpha:1];
    
    // self.buttonView.backgroundColor = [UIColor yellowColor];
    //self.leftButton.backgroundColor = [UIColor purpleColor];
    // self.rightButton.backgroundColor = [UIColor redColor];
    // self.leftButton.alpha = 0;
    //self.rightButton.alpha = 0;
    
    
    
    [self.leftButton addTarget:self action:@selector(appreView1:)forControlEvents:UIControlEventTouchUpInside];
    
    [self.rightButton addTarget:self action:@selector(appreView2:)forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.buttonView];
    [self.buttonView addSubview:underButtonView];
    [underButtonView addSubview:self.backView];
    [underButtonView addSubview:self.leftButton];
    [underButtonView addSubview:self.rightButton];
}

- (void)appreView1:(UIButton *)leftbutton
{
    // NSLog(@"111");
    self.rightButton.tintColor = [UIColor blackColor];
    self.leftButton.tintColor = [UIColor blueColor];
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.center = self.leftButton.center;
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view2.frame = CGRectMake(WIDTH, 104, WIDTH, HIGHT - 104 - 49);
        self.view1.frame = CGRectMake(0, 104, WIDTH, HIGHT - 104 - 49);
    }];
}


- (void)appreView2:(UIButton *)rightbutton
{
    //  NSLog(@"222%f",self.tabBarController.tabBar.frame.size.heigh
    self.rightButton.tintColor = [UIColor blueColor];
    self.leftButton.tintColor = [UIColor blackColor];
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.center = self.rightButton.center;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.view1.frame = CGRectMake(- WIDTH, 104, WIDTH, HIGHT - 104 - 49);
            self.view2.frame = CGRectMake(0, 104, WIDTH, HIGHT - 104 - 49);
        }];
        
    }];
}


//创建view1 和view2 来装两个tableview

- (void)createView1AndView2
{
    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 104, WIDTH, HIGHT - 104 - 49)];
    self.view1.backgroundColor = [UIColor purpleColor];
    
    
    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 104, WIDTH, HIGHT - 104 - 49)];
     self.view2.backgroundColor = [UIColor redColor];
    
    
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
